define(function(require, exports, module){
  'use strict';
  var $ = require('jquery');
  require('bootstrapjs')($);
  // require('nicescroll')($);
  require('icheck')($);
  var fnModal = require('seajs/fn/fnModal');
  var _const = require('seajs/fn/fnConst.js');
  var fnUtils = require('seajs/fn/fnUtils');
  var SimpleMap = require('seajs/plugs/simpleMap');

  // common start 
  var checkboxInit = function(){
    $('input[name="bc_group_chk"]').iCheck({
      checkboxClass: 'boardcast-select-checkbox',
      increaseArea: '12%'
    });
  }

  var Boardcast = function(){
  	this.ws = undefined;
    this.selectedMap = undefined;
    this.isCreated = undefined;
    this.groupName=undefined;
    this.init();
  }

  Boardcast.prototype.init = function(){
    var self = this;
    self.isCreated = false;
    self.selectedMap = new SimpleMap();
    // bottom click
    //$('a[id="temporary_broadcast"] > button').on('click', function(){
    //  self.initBCDialog();
    //  self.initSelectDialog(undefined);
    //  self.openBCDialog();
    //});
    $('#temporary_broadcast').on('click', function(){
      if(self.ws.tasks.currentId === undefined){
        self.initBCDialog();
        self.initSelectDialog(undefined);
        self.openBCDialog();
      }else{
        var status=$('#main_bottom_left_voice_obj').html().slice(0,-1);
        $('#function-alert').html(status+'正在进行,请先关闭'+status+'组在进行其他操作').fadeIn(200,function(){
          setTimeout(function(){
            $('#function-alert').fadeOut(500);
          },1000)
        })
      }
    });
  }

  Boardcast.prototype.setWs = function(ws){
    this.ws = ws;
  }
 //点击广播按钮 动态生成选择列表
  Boardcast.prototype.drawBCLi = function(){
    if(this.ws.slidebar){
      var contentHtml = [];
      for(var i in this.ws.slidebar.groupIds.enable){
        contentHtml.push('<li id="bc_li_' + this.ws.slidebar.groupIds.enable[i] + '"><a href="#"><img src="images/group.png">' 
          + this.ws.slidebar.groupMap.get(this.ws.slidebar.groupIds.enable[i]).groupName
          + '<input type="checkbox" name="bc_group_chk" id="bc_group_chk_' 
          + this.ws.slidebar.groupIds.enable[i] + '" value="' +
          + this.ws.slidebar.groupIds.enable[i] + '"/></a></li>');
      }
      $('#bc_ul').html(contentHtml.join(''));
    }
  }

  Boardcast.prototype.onEvent = function(){
    var self = this;
    checkboxInit();
    //发起广播时选择群组显示已选中多少个群组，监听被选中群组状态
    $('input[name="bc_group_chk"]').on('ifChanged', function(){
      var checkNum = 0;
      if($(this).is(':checked')){
        self.selectedMap.put($(this).val(), $(this).val());
      }else{
        self.selectedMap.remove($(this).val());
      }
      $('input[name="bc_group_chk"]').each(function(){
        if($(this).is(':checked')){
          checkNum++;
        }
      });
      if(checkNum === self.ws.slidebar.groupIds.enable.length){
        $('#bc_lable').html('已选择全部群组');
      }else{
        $('#bc_lable').html('已选择' + checkNum + '个群组');
      }
      if(checkNum === 0){
        $('#bc_btn').attr('disabled','disabled');
      }else{
        $('#bc_btn').removeAttr('disabled');
      }
    });
    //input 选择状态
    $('input[name="bc_group_chk"]').iCheck('check');
   
    $('#bc_small').on('click', function(){
      $('#boardcast').modal('hide');
    });
    // $('#bc_dismiss, #bc_btn').on('click', function(){
    //   $('#bc_end_modal').modal('show');
    // });
    //点击叉叉 隐藏
    $('#bc_select_dismiss').on('click', function(){
      self.enabledOpenBtn();
      $('#boardcast').modal('hide');
    });
    $('#bc_select_error_x').on('click', function(){
      self.hideAlertError();
    });
    $('#bc_select_error_btn').on('click', function(){
      self.hideAlertError();
    });
  }

  Boardcast.prototype.initBCDialog = function(){
    this.drawBCLi();
    this.onEvent();
  }

  Boardcast.prototype.openBCDialog = function(){
    this.disabledOpenBtn();
    $('#boardcast').modal('show');
  }

  Boardcast.prototype.initSelectDialog = function(checkedMap){
    var self = this;
    $('#bc_select_dismiss').css('display', 'block');
    // $('#bc_dismiss').css('display', 'none');
    $('#bc_small').css('display', 'none');
    $('.boardcast-select-checkbox').css('display', 'inline-block');
    $('#bc_lable').css('display', 'block');
    $('#start_bc_btn').css('display', 'block');
    $('#end_bc_btn').css('display', 'none');
    self.startBCBtn();
    if(checkedMap){
      $('input[name="bc_group_chk"]').iCheck('uncheck');
      for(var i in checkedMap.map){
        $('#bc_group_chk_' + i).iCheck('check');
      }
      self.initBoardcastDialog();
      $('#bc_title').html('广播中(' +  checkedMap.size() + '个)');
    }
  }

  Boardcast.prototype.initBoardcastDialog = function(){
    var self = this;
    $('#bc_select_dismiss').css('display', 'none');
    // $('#bc_dismiss').css('display', 'block');
    $('#bc_small').css('display', 'block');
    $('.boardcast-select-checkbox').css('display', 'none');
    $('#bc_lable').css('display', 'none');
    $('#start_bc_btn').css('display', 'none');
    $('#end_bc_btn').css('display', 'block');
    $('.modal-backdrop').css('display','none');
    //循环所有组，不在列表中则做隐藏处理
    var groups = [];
    for(var j in self.ws.slidebar.groupMap.map){
      var contains = false;
      for(var i in self.selectedMap.map){
        if(i === j){
          contains = true;
          continue;
        }
      }
      if(!contains){
        groups.push(j);
      }
      
    }
    for(var m in groups){
      $('#bc_li_' + groups[m]).remove();
    }
    self.endBCBtn();
  }
//发起广播
  Boardcast.prototype.startBCBtn = function(){
    var self = this;

    $('#bc_btn').off('mouseup').on('mouseup', function(){

      self.groupName=$('#main_bottom_left_voice_obj').html();

      $('#bc_title').html('广播中(' +  self.selectedMap.size() + '个)');
      self.createBC(function(rlt){
        self.ws.boardcast(rlt.body);
      }, function(e){
        self.hideAlert();
        self.hideAlertError();
      });
    }).removeClass('btn-danger').addClass('btn-primary').html('发起广播');
  }
//结束广播
  Boardcast.prototype.endBCBtn = function(){
    var self = this;
    $('#bc_btn').off('mouseup').on('mouseup', function(){
      $('#confirmInfo').html('是否确定结束当前广播?'); 
      $('#confirm_btn').off('click').on('click', function(){
        self.ws.boardCastHangUp(self.ws.cid, self.ws.sid, self.ws.tasks.currentId,self.groupName);
      });
      $('#confirm_modal').modal('show');
    }).removeClass('btn-primary').addClass('btn-danger').html('结束广播');
  }

  Boardcast.prototype.disabledOpenBtn = function(){
    $('.broadcast_btn').attr('disabled', 'disabled');
    //$('#temporary_broadcast1').css('color','#ddd');
    //$('#temporary_broadcast1 img').attr('src','images/untemporary_broadcast.png');
    //$('#temporary_talking1').css('color','#ddd');
    //$('#temporary_talking1 img').attr('src','images/untemporary_talking.png');
  }

  Boardcast.prototype.enabledOpenBtn = function(){
    $('.broadcast_btn').removeAttr('disabled');
    //$('#temporary_broadcast1').css('color','#3399ff');
    //$('#temporary_broadcast1 img').attr('src','images/temporary_broadcast.png');
    //$('#temporary_talking1').css('color','#3399ff');
    //$('#temporary_talking1 img').attr('src','images/temporary_talking.png');
  }

  Boardcast.prototype.showAlert = function(msg){
    $('#bc_select_mask').css('display', 'block');
    $('#bc_select_alert').html(msg);
    $('#bc_select_alert').css('display', 'block');
  }

  Boardcast.prototype.hideAlert = function(){
    $('#bc_select_mask').css('display', 'none');
    $('#bc_select_alert').html('');
    $('#bc_select_alert').css('display', 'none');
  }

  Boardcast.prototype.showAlertError = function(msg){
    $('#bc_select_mask').css('display', 'block');
    $('#bc_select_error_info').html(msg);
    $('#bc_select_error').css('display', 'block');
  }

  Boardcast.prototype.hideAlertError = function(){
    $('#bc_select_mask').css('display', 'none');
    $('#bc_select_error_info').html('');
    $('#bc_select_error').css('display', 'none');
  }

  Boardcast.prototype.createBC = function(success, error){
    var self = this;
    var data = {
      'userid': parseInt(self.ws.uid),
      'userType': _const.DEVICE.DISPATCHER,//_const.DEVICE.DISPATCHER=1 设备类型 1表示调度台
      'companyid': parseInt(self.ws.cmpid),
      'timestamp': fnUtils.getUnixTime() + '',
      'key': self.ws.key,
      'groupType': _const.PTT_TYPE.BOARDCAST, //_const.PTT_TYPE.BOARDCAST=3
      'groupIds': self.selectedMap.keySet().join(',')
    }
    self.showAlert('正在发起广播,请稍等!');
    fnUtils.commonPost('group/createTemporaryGroup4Broadcast.do', data,
      function(rlt){
        if(rlt.status === _const.HTTP.RES_STATUS.SUCCESS){
          if(rlt.error.code === _const.HTTP.RES_STATUS.ERROR_CODE.FAIL){//_const.HTTP.RES_STATUS.ERROR_CODE.FAIL=1001
            if(error){
              error(rlt.error.message);
            }
          }else{
            setTimeout(function(){
              success(rlt);
            }, 2000);
          }
        }else{
          if(error){
            error(rlt.error.message);
          }
        }
      },
      function(e){
        error(e);
      }
    );

  }

  return Boardcast;

});